package com.lxmes.technology.service.impl;

import java.util.List;

import com.lxmes.common.core.domain.entity.SysDictData;
import com.lxmes.common.core.domain.entity.SysUser;
import com.lxmes.common.exception.ServiceException;
import com.lxmes.common.utils.SecurityUtils;
import com.lxmes.common.utils.StringUtils;
import com.lxmes.common.utils.bean.BeanValidators;
import com.lxmes.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lxmes.technology.mapper.NcProcessTecMapper;
import com.lxmes.technology.domain.NcProcessTec;
import com.lxmes.technology.service.INcProcessTecService;

import javax.validation.Validator;

/**
 * 工序工艺Service业务层处理
 *
 * @author ruoyi
 * @date 2022-11-23
 */
@Service
public class NcProcessTecServiceImpl implements INcProcessTecService
{
    @Autowired
    private NcProcessTecMapper ncProcessTecMapper;

    @Autowired
    protected Validator validator;
    @Autowired
    private SysUserMapper userMapper;

    /**
     * 查询工序工艺
     *
     * @param id 工序工艺主键
     * @return 工序工艺
     */
    @Override
    public NcProcessTec selectNcProcessTecById(Long id)
    {
        return ncProcessTecMapper.selectNcProcessTecById(id);
    }

    /**
     * 查询工序工艺列表
     *
     * @param ncProcessTec 工序工艺
     * @return 工序工艺
     */
    @Override
    public List<NcProcessTec> selectNcProcessTecList(NcProcessTec ncProcessTec)
    {
        /*List<NcProcessTec> ncList = ncProcessTecMapper.selectNcProcessTecList(ncProcessTec);
        for (NcProcessTec tec:ncList){
            SysUser user = userMapper.selectUserByJobNo(tec.getPgGwFzr());
            tec.setPgGwFzr(user.getUserId().toString());
            tec.setPgGwFzr1(user.getJobNo());
            tec.setPgGwFzr2(user.getUserName());
            if(StringUtils.isNotEmpty(tec.getGwPersonInfo())){
                String[] ncList2 = tec.getGwPersonInfo().split(",");
                StringBuffer s= new StringBuffer();
                StringBuffer s1= new StringBuffer();
                StringBuffer s2= new StringBuffer();
                for (int i = 0; i <ncList2.length ; i++) {
                    SysUser user2 = userMapper.selectUserByJobNo(ncList2[i]);
                    s.append(user2.getUserId().toString()).append(",");
                    s1.append(user2.getJobNo()).append(",");
                    s2.append(user2.getUserName()).append(",");
                }
                tec.setGwPersonInfo(s.toString().substring(0,s.length()-1));
                tec.setGwPersonInfo1(s1.toString().substring(0,s1.length()-1));
                tec.setGwPersonInfo2(s2.toString().substring(0,s2.length()-1));
            }
            ncProcessTecMapper.updateNcProcessTec(tec);
        }*/
        return ncProcessTecMapper.selectNcProcessTecList(ncProcessTec);
    }



    /**
     * 新增工序工艺
     *
     * @param ncProcessTec 工序工艺
     * @return 结果
     */
    @Override
    public int insertNcProcessTec(NcProcessTec ncProcessTec)
    {
        if(StringUtils.isEmpty(ncProcessTec.getPgGwName())){
            ncProcessTec.setPgGwName(ncProcessTec.getStationName());
        }
        ncProcessTec.setPgGwCode(ncProcessTec.getStationCode());
        if(StringUtils.isNotEmpty(ncProcessTec.getGwPersonInfo())){
            String[] str = ncProcessTec.getGwPersonInfo().split(",");
            ncProcessTec.setGwPersonNum((long)str.length);
        }else {
            ncProcessTec.setGwPersonNum(1L);
        }
        return ncProcessTecMapper.insertNcProcessTec(ncProcessTec);
    }

    /**
     * 修改工序工艺
     *
     * @param ncProcessTec 工序工艺
     * @return 结果
     */
    @Override
    public int updateNcProcessTec(NcProcessTec ncProcessTec)
    {
        if(StringUtils.isEmpty(ncProcessTec.getPgGwName())){
            ncProcessTec.setPgGwName(ncProcessTec.getStationName());
        }
        ncProcessTec.setPgGwCode(ncProcessTec.getStationCode());
        if(StringUtils.isNotEmpty(ncProcessTec.getGwPersonInfo())){
            String[] str = ncProcessTec.getGwPersonInfo().split(",");
            ncProcessTec.setGwPersonNum((long)str.length);
        }else {
            ncProcessTec.setGwPersonNum(0L);
        }
        return ncProcessTecMapper.updateNcProcessTec(ncProcessTec);
    }

    /**
     * 批量删除工序工艺
     *
     * @param ids 需要删除的工序工艺主键
     * @return 结果
     */
    @Override
    public int deleteNcProcessTecByIds(Long[] ids)
    {
        return ncProcessTecMapper.deleteNcProcessTecByIds(ids);
    }

    /**
     * 删除工序工艺信息
     *
     * @param id 工序工艺主键
     * @return 结果
     */
    @Override
    public int deleteNcProcessTecById(Long id)
    {
        return ncProcessTecMapper.deleteNcProcessTecById(id);
    }

    @Override
    public String imporIteminfo(List<NcProcessTec> tecList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(tecList) || tecList.size() == 0)
        {
            throw new ServiceException("导入工位信息数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (NcProcessTec tecIfo : tecList)
        {
            try
            {
                NcProcessTec tecinfoSearch = new NcProcessTec();
                tecinfoSearch.setStationCode(tecIfo.getStationCode());
                NcProcessTec ncProcessTecs = ncProcessTecMapper.selectNcProcessTecBysCode(tecinfoSearch);
                if (StringUtils.isNull(ncProcessTecs))
                {
                    BeanValidators.validateWithException(validator, tecIfo);
                    tecIfo.setCreateBy(operName);
                    tecIfo.setUpdateBy(operName);
                    tecIfo.setFactoryCode("WR");
                    tecIfo.setFactoryName("玮瑞");
                    if(StringUtils.isNotEmpty(tecIfo.getPgGwFzr1())) {
                        SysUser user = userMapper.selectUserByJobNo(tecIfo.getPgGwFzr1());
                        if(StringUtils.isNull(user)){
                            failureNum++;
                            String msg = "<br/>" + tecIfo.getStationName()+ " 导入失败：";
                            failureMsg.append(msg+"用户工号未匹配到用户！");
                            continue;
                        }
                        tecIfo.setPgGwFzr(user.getUserId().toString());
                        tecIfo.setPgGwFzr1(user.getJobNo());
                        tecIfo.setPgGwFzr2(user.getUserName());
                        if (StringUtils.isNotEmpty(tecIfo.getGwPersonInfo1())) {
                            String[] ncList2 = tecIfo.getGwPersonInfo1().split(",");
                            StringBuffer s = new StringBuffer();
                            StringBuffer s1 = new StringBuffer();
                            StringBuffer s2 = new StringBuffer();
                            for (int i = 0; i < ncList2.length; i++) {
                                SysUser user2 = userMapper.selectUserByJobNo(ncList2[i]);
                                s.append(user2.getUserId().toString()).append(",");
                                s1.append(user2.getJobNo()).append(",");
                                s2.append(user2.getUserName()).append(",");
                            }
                            tecIfo.setGwPersonNum((long)s.length());
                            tecIfo.setGwPersonInfo(s.toString().substring(0, s.length() - 1));
                            tecIfo.setGwPersonInfo1(s1.toString().substring(0, s1.length() - 1));
                            tecIfo.setGwPersonInfo2(s2.toString().substring(0, s2.length() - 1));
                        }else{
                            tecIfo.setGwPersonNum(1L);
                        }
                    }else {
                        failureNum++;
                        String msg = "<br/>" + tecIfo.getStationName()+ " 导入失败：";
                        failureMsg.append(msg+"负责人不能为空！");
                        continue;
                    }
                    this.insertNcProcessTec(tecIfo);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、工位 " + tecIfo.getStationName() + " 导入成功");
                }
                else
                {
                    BeanValidators.validateWithException(validator, tecIfo);
                    tecIfo.setUpdateBy(operName);
                    this.updateNcProcessTec(tecIfo);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、数据 " + tecIfo.getStationName()+ " 更新成功");
                }


            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + tecIfo.getStationName()+ " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
